package com.example.card;

import com.example.card.domain.card.Card;
import com.example.card.domain.order.AddOilOrder;
import com.example.card.domain.user.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class CardDataBase {

    private static final Map<Integer, Card> cardMap = new HashMap<>();
    private static final Map<Integer, List<Integer>> memberCardsMap = new HashMap<>();

    private static final Map<Integer, User> userMap = new HashMap<>();
    private static final Map<Integer, List<AddOilOrder>> oilOrderMap = new HashMap<>();

    public static void addCard(Card card) {
        cardMap.put(card.getId(), card);
    }

    public static Card getCard(Integer id) {
        return cardMap.get(id);
    }

    public static void bindCard(int memberId, int cardId) {
        List<Integer> cardIds = getCardIdsList(memberId);
        cardIds.add(cardId);
        memberCardsMap.put(memberId, cardIds);
    }

    public static void unbindCard(int memberId, int cardId) {
        List<Integer> cardIds = getCardIdsList(memberId);
        cardIds.remove(cardId);
        memberCardsMap.put(memberId, cardIds);
    }

    public static List<Card> getCardsList(int memberId) {
        List<Integer> cardIdsList = getCardIdsList(memberId);
        return cardIdsList.stream().map(id -> cardMap.get(id)).collect(Collectors.toList());
    }

    public static List<Integer> getCardIdsList(int memberId) {
        List<Integer> cardIds = memberCardsMap.get(memberId);
        if (cardIds == null) {
            cardIds = new ArrayList<>();
        }
        return cardIds;
    }

    public static User getUser(int memberId) {
        return userMap.get(memberId);
    }

    public static void addUser(User user) {
        userMap.put(user.getId(), user);
    }

    public static void saveAddOilOrder(AddOilOrder addOilOrder) {
        List<AddOilOrder> addOilOrderList = getAddOilOrderList(addOilOrder.getMemberId());
        addOilOrderList.add(addOilOrder);
        oilOrderMap.put(addOilOrder.getMemberId(), addOilOrderList);
    }

    public static List<AddOilOrder> getAddOilOrderList(Integer memberId) {
        List<AddOilOrder> addOilOrders = oilOrderMap.get(memberId);
        if (addOilOrders == null) {
            addOilOrders = new ArrayList<>();
        }
        return addOilOrders;
    }
}
